<?php

namespace app\common\model;

use think\Model;
use app\common\model\Base;
use think\Db;
use traits\model\SoftDelete;

class Goods extends Base {

    use SoftDelete;

    protected $deleteTime = 'delete_time';

    public function getRecommendAttr($value) {
        $status = [0 => '', 1 => '是'];
        return isset($status[$value]) ? $status[$value] : '';
    }

    public function getCategoryNameAttr($value, $data) {
        return db('goods_cat')->where('id', $data['cid'])->value('title');
    }

    public function getStatusTextAttr($value, $data) {
        return $data['status'] == 0 ? '下架' : '上架';
    }

    public function tags_update($post, $id) {

        // 更新标签
        db('goods_tags')->where('goods_id', $id)->delete();

        $tags = isset($post['tags']) ? $post['tags'] : [];

        $tags_arr = explode(' ', $tags);
        foreach ($tags_arr as $key => $value) {
            if ($value)
                db('goods_tags')->insert([
                    'title' => $value,
                    'goods_id' => $id,
                ]);
        }
    }

    public function albums_update($post, $id) {



        // 更新图集        
        db('goods_albums')->where('goods_id', $id)->delete();

        $img_arr = isset($post['image_ids']) ? $post['image_ids'] : [];



        foreach ($img_arr as $key => $value) {
            //有图片才添加
            if ($value['image']) {

                db('goods_albums')->insert([
                    'goods_id' => $id,
                    'image' => $value['image'],
                    'description' => $value['description'],
                    'listorder' => $value['listorder']
                ]);
            }
        }
    }

    public function edit_submit($post, $id, $extra_common = []) {


        Db::startTrans();
        try {


            // 商品的补充字段
            $smeta_arr = [];
            $extra_fields = config('smeta_json_goods');
            if ($extra_fields && is_array($extra_fields)) {
                foreach ($extra_fields as $key => $value) {
                    $smeta_arr[$key] = $post[$key] ?? '';
                }
            }
            $smeta_json = json_encode($smeta_arr);


            $data = [
                'title' => $post['title'] ?? '',
                'content' => $post['content'] ?? '',
                'cid' => $post['cid'] ?? '',
                'image' => $post['image'] ?? '',
                'keywords' => $post['keywords'] ?? '',
                'description' => $post['description'] ?? '',
                'tpl_views' => $post['tpl_views'] ?? '',
                'alias' => $post['alias'] ?? '',
                'quantity' => $post['quantity'] ?? '',
                'status' => $post['status'] ?? '',
                'price' => $post['price'] ?? '',
                'points' => $post['points'] ?? '',
                'pay_points' => $post['pay_points'] ?? '',
                'price_market' => $post['price_market'] ?? '',
                'update_time' => time(),
                'smeta_json' => $smeta_json
            ];

            db('goods')->where('id', $id)->update($data);

            foreach ($extra_common as $method) {
                if (method_exists($this, $method)) {
                    $this->$method($post, $id);
                }
            }


            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $e->getMessage();
        }
    }

    public function add_submit($post, $extra_common = []) {

        Db::startTrans();
        try {



            // 商品的补充字段
            $smeta_arr = [];
            $extra_fields = config('smeta_json_goods');
            if ($extra_fields && is_array($extra_fields)) {
                foreach ($extra_fields as $key => $value) {
                    $smeta_arr[$key] = $post[$key] ?? '';
                }
            }
            $smeta_json = json_encode($smeta_arr);


            $data = [
                'title' => $post['title'] ?? '',
                'content' => $post['content'] ?? '',
                'cid' => $post['cid'] ?? '',
                'image' => $post['image'] ?? '',
                'keywords' => $post['keywords'] ?? '',
                'description' => $post['description'] ?? '',
                'tpl_views' => $post['tpl_views'] ?? '',
                'alias' => $post['alias'] ?? '',
                'quantity' => $post['quantity'] ?? '',
                'status' => $post['status'] ?? '',
                'price' => $post['price'] ?? '',
                'points' => $post['points'] ?? '',
                'pay_points' => $post['pay_points'] ?? '',
                'price_market' => $post['price_market'] ?? '',
                'create_time' => time(),
                'update_time' => time(),
                'smeta_json' => $smeta_json
            ];
            $insert_id = db('goods')->strict(TRUE)->insertGetId($data);

            foreach ($extra_common as $method) {
                if (method_exists($this, $method)) {
                    $this->$method($post, $insert_id);
                }
            }

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $e->getMessage();
        }
    }

    public function model_where($wheres = []) {

        foreach ($wheres as $key => $value) {
            if (isset($value[1]))
                $this->where($key, $value[0], $value[1]);
            else
                $this->where($key, $value[0]);
        }

        if (request()->get('keyword')  !== false)
            $this->where('title', 'like', '%' . request()->get('keyword') . '%');
        
        
        if(request()->get('points') !== false)
            $this->where('pay_points' , '>', request()->get('points'));

        if (request()->get('category') )
            $this->where('cid', request()->get('category'));

        return $this;
    }

}
